package com.card.game;

import org.jboss.netty.channel.ChannelHandlerContext;

import com.card.common.CardContants;
import com.card.common.MessageComm;
import com.card.game.ddz.DdzPlayer;
import com.card.game.ddz.DdzRoom;
import com.card.game.dn.NnRoom;
import com.card.game.event.EventType;
import com.card.game.pdk.PdkPlayer;
import com.card.game.pdk.PdkRoom;
import com.card.game.room.AbstractRoom;
import com.card.game.room.RoomState;
import com.card.game.room.SHRoom;
import com.card.game.room.TggRoom;
import com.card.user.User;
import com.card.utils.Utils;
import com.xiaoao.net.json.IMsgProcesser;
import com.xiaoao.net.json.Router;
import com.xiaoao.net.json.message.JsonMessage;

public class GameLogic implements IMsgProcesser{
	private static final GameLogic instance = new GameLogic();
    public static GameLogic getInstance() {
        return instance;
    }
	@Override
	public void registMsgId() {
		Router.getInstance().registMsg(MessageComm.C2S_HandOut, this);
		Router.getInstance().registMsg(MessageComm.C2S_DdzHandOut, this);
		Router.getInstance().registMsg(MessageComm.C2S_PdkHandOut, this);
		Router.getInstance().registMsg(MessageComm.C2S_DoSkill, this);
		Router.getInstance().registMsg(MessageComm.C2S_Bet, this);
		Router.getInstance().registMsg(MessageComm.C2S_TggFaPaiComplete, this);
		Router.getInstance().registMsg(MessageComm.C2S_TggRank, this);
		Router.getInstance().registMsg(MessageComm.C2S_ZjhSkill, this);
		Router.getInstance().registMsg(MessageComm.C2S_DdzQiangDizhu, this);
		Router.getInstance().registMsg(MessageComm.C2S_ShowNiu, this);
		Router.getInstance().registMsg(MessageComm.C2S_Niu_Bet, this);
	}

	@Override
	public void doProcess(int msgId, JsonMessage serverMsg, ChannelHandlerContext ctx) {
		switch (msgId) {
			case MessageComm.C2S_HandOut:
				handOut(serverMsg, ctx);
				break;
			case MessageComm.C2S_DdzHandOut:
				ddzHandOut(serverMsg, ctx);
				break;
			case MessageComm.C2S_PdkHandOut:
				pdkHandOut(serverMsg, ctx);
				break;
			case MessageComm.C2S_DoSkill:
				doSkill(serverMsg, ctx);
				break;
			case MessageComm.C2S_Bet:
				bitAction(serverMsg, ctx);
				break;
			case MessageComm.C2S_TggFaPaiComplete:
				tggFapaiComplete(serverMsg, ctx);
				break;
			case MessageComm.C2S_TggRank:
				getTggRoomRankScore(serverMsg, ctx);
				break;
			case MessageComm.C2S_ZjhSkill:
				zjhSkill(serverMsg, ctx);
				break;
			case MessageComm.C2S_DdzQiangDizhu:
				qiangDizhu(serverMsg, ctx);
				break;
			case MessageComm.C2S_ShowNiu:
				baoNiu(serverMsg, ctx);
				break;
			case MessageComm.C2S_Niu_Bet:
				niuBet(serverMsg, ctx);
				break;
		}
	}
	// 出牌
	private void handOut(JsonMessage serverMsg, ChannelHandlerContext ctx) {
		User user = (User)ctx.getAttachment();
		AbstractRoom room = user.getRoom();
//		if(room.getState() != RoomState.Running) {
//			Global.logErrorMessage("当前房间状态不正确！！");
//			return;
//		}
		
		Card card = new Card(serverMsg.getAsInt("type"), serverMsg.getAsInt("index"));// 获取牌的花色和值
		room.handOut(user.getPrimaryKey(), card,serverMsg.getAsBoolean("ting"));
	}
	
	private void ddzHandOut(JsonMessage serverMsg, ChannelHandlerContext ctx) {
		User user = (User)ctx.getAttachment();
		AbstractRoom room = user.getRoom();
		
		DdzRoom ddzRoom = (DdzRoom)room;
		DdzPlayer player = ddzRoom.getPlayerById(user.getPrimaryKey());
		String cardsStr = serverMsg.getAsString("outCards");
		ddzRoom.handOut(player, Utils.parseCards(cardsStr));
	}
	//跑得快出牌
	private void pdkHandOut(JsonMessage serverMsg, ChannelHandlerContext ctx) {
		User user = (User)ctx.getAttachment();
		AbstractRoom room = user.getRoom();
		
		PdkRoom pdkRoom = (PdkRoom)room;
		PdkPlayer player = pdkRoom.getPlayerById(user.getPrimaryKey());
		String cardsStr = serverMsg.getAsString("outCards");
		pdkRoom.handOut(player, Utils.parseCards(cardsStr));
	}
	
	private void doSkill(JsonMessage serverMsg, ChannelHandlerContext ctx) {
		User user = (User)ctx.getAttachment();
		SHRoom room = (SHRoom)user.getRoom();
//		if(room.getState() != RoomState.Running) {
//			Global.logErrorMessage("技能：当前房间状态不正确！！");
//			return;
//		}
//		AbstractRoom room2 = (SHRoom)user.getRoom();
//		AbstractRoom room3 = RoomManager.getInstance().getRoomById(user.getRoomId());
//		System.err.println("room2:"+room2.getTableCards());
//		System.err.println("room:"+room.getTableCards());
//		System.err.println("room3:"+room3.getTableCards());
		EventType type = EventType.valueOf(serverMsg.getAsString("type"));
		int cardIndex = serverMsg.getAsInt("chiIndex");
		room.doSkill(user.getPrimaryKey(), type,cardIndex);
	}
	
	private void zjhSkill(JsonMessage serverMsg, ChannelHandlerContext ctx) {
		User user = (User)ctx.getAttachment();
		TggRoom room = (TggRoom)user.getRoom();
		EventType type = EventType.valueOf(serverMsg.getAsString("type"));
		room.doSkill(user.getPrimaryKey(), type,serverMsg);
	}
	private void qiangDizhu(JsonMessage serverMsg, ChannelHandlerContext ctx) {
		User user = (User)ctx.getAttachment();
		try {
			user.getLock().lock();
			AbstractRoom room = user.getRoom();
			if(room == null) {
				return;
			}
			if(room.getRoomType() == CardContants.GAMETYPE_Ddz) {
				int qiang = serverMsg.getAsInt("qiang");
				boolean flag = false ;
				if(qiang==0){
					flag = false ;
				}
				else {
					flag = true ;
				}
//				boolean qiang = serverMsg.getAsInt("qiang") == 1;
				DdzRoom ddzRoom = (DdzRoom)room;
				DdzPlayer player = ddzRoom.getPlayerById(user.getPrimaryKey());
				ddzRoom.qiangDizhu(player, flag ,qiang);
				return;
			}
			if(room.getRoomType() == CardContants.GAMETYPE_Pdk) {
				boolean qiang = serverMsg.getAsInt("qiang") == 1;
				PdkRoom pdkRoom = (PdkRoom)room;
				PdkPlayer player = pdkRoom.getPlayerById(user.getPrimaryKey());
				pdkRoom.qiangDizhu(player, qiang);
				return;
			}
			if(room.getRoomType() == CardContants.GAMETYPE_Nn) {
				int qiang = serverMsg.getAsInt("qiang");
				NnRoom nnRoom = (NnRoom)room;
				nnRoom.qiangZhuang(user.getPrimaryKey(), qiang);
				return;
			}
		} catch (Exception e) {
		} finally {
			user.getLock().unlock();
		}
	}
	private void baoNiu(JsonMessage serverMsg, ChannelHandlerContext ctx) {
		User user = (User)ctx.getAttachment();
		AbstractRoom room = user.getRoom();
		if(room == null) {
			return;
		}
		if(room.getRoomType() == CardContants.GAMETYPE_Nn) {
			NnRoom nnRoom = (NnRoom)room;
			nnRoom.niuAction(user.getPrimaryKey());
			return;
		}
	}
	private void niuBet(JsonMessage serverMsg, ChannelHandlerContext ctx) {
		User user = (User)ctx.getAttachment();
		AbstractRoom room = user.getRoom();
		if(room == null) {
			return;
		}
		if(room.getRoomType() == CardContants.GAMETYPE_Nn) {
			NnRoom nnRoom = (NnRoom)room;
			int score = serverMsg.getAsInt("score");
			nnRoom.betAction(user.getPrimaryKey(), score);
			return;
		}
	}
	
	private void bitAction(JsonMessage serverMsg, ChannelHandlerContext ctx) {
//		User user = (User)ctx.getAttachment();
//		TggRoom room = (TggRoom)user.getRoom();
//		if(room == null) {
//			return;
//		}
//		if(room.getState() != RoomState.WaitBet) {
////			JsonMessageCenter.getInstance().sendEorrorMessage(user, "无效下注");
//			return;
//		}
//		if(!room.isCanBet(user.getPrimaryKey())) {
//			JsonMessageCenter.getInstance().sendEorrorMessage(user, "您不能下注");
//			return;
//		}
//		
//		int index = serverMsg.getAsInt("index");
//		int bet = serverMsg.getAsInt("bet");
//		Player player = room.getPlayerById(user.getPrimaryKey());
//		if(player == null) {
////			JsonMessageCenter.getInstance().sendEorrorMessage(user, "无效下注");
//			return;
//		}
//		int maxValue = room.getRoomSetting().getBetLimit();
//		int curBet = room.getPlayerBet(user.getPrimaryKey()).getBetValueByIndex(index);
//		if(maxValue > 0 && curBet+bet > maxValue) {
//			JsonMessageCenter.getInstance().sendEorrorMessage(user, "最大下注为:"+maxValue+",您已超额");
//			return;
//		}
//		room.addBet(player,index, bet);
	}
	
	private void tggFapaiComplete(JsonMessage serverMsg, ChannelHandlerContext ctx) {
		User user = (User)ctx.getAttachment();
		TggRoom room = (TggRoom)user.getRoom();
		if(room == null || room.getState() != RoomState.OpenPai) {
			return;
		}
		BasePlayer player = room.getPlayerById(user.getPrimaryKey());
		if(player == null) {
			return;
		}
//		player.setState(UserState.FapaiComplete);
//		if(room.allIsState(UserState.FapaiComplete)) {
//			room.calScore();
//		}
	}
	
	private void getTggRoomRankScore(JsonMessage serverMsg, ChannelHandlerContext ctx) {
		User user = (User)ctx.getAttachment();
		TggRoom room = (TggRoom)user.getRoom();
		if(room == null) {
			return;
		}
//		List<TggResult> ranks = room.getTggResults().getSortRank();
//		JsonMessage message = JsonMessage.createMessage(MessageComm.S2C_TggRank);
//		message.addProperty("ranks", ranks);
//		JsonMessageCenter.getInstance().sendMessage(message, ctx);
	}
}
